<template>
  <div class="search">
    <div class="search-input">
      <input type="text" v-model="input" />
      <div><span @click="handleSearch()"><a href="##">搜索</a></span></div>
    </div>
    <div class="search-conent">
      <ul class="tab-tilte">
        <li @click="cur = 0" :class="{ active: cur == 0 }">单曲</li>
        <li @click="cur = 1" :class="{ active: cur == 1 }">歌手</li>
        <li @click="cur = 2" :class="{ active: cur == 2 }">歌单</li>
        <li @click="cur = 3" :class="{ active: cur == 3 }">视频</li>
      </ul>
      <div class="tab-content">
        <div v-show="cur == 0">
          <div class="single" v-for="item of single" :key="item.id">
            <div class="name">
              <a href="##" @click="handleDetail(item.id)">{{
                item.name | handleString
              }}</a>
              <div class="box">
                <a href="##">
                  <span
                    class="iconfont icon-jiahao"
                    @click="handlePaly(item)"
                  ></span
                ></a>
              <a href="##">
                  <span class="iconfont icon-xiai"
                  @click="handleCollect(item)"
                  ></span>
              </a>
              </div>
            </div>
            <div class="singer">{{ item.artists[0].name }}</div>
            <div class="album">{{ item.album.name | handleString }}</div>
            <div class="time">{{ item.duration | handleTime }}</div>
          </div>
        </div>
        <div v-show="cur == 1">
          <div class="song">
            <div
              class="song-item"
              v-for="item of song"
              :key="item.id"
              @click="handleSinger(item.id)"
            >
              <img v-lazy="item.picUrl" alt="" />
              <p>{{ item.name }}</p>
            </div>
          </div>
        </div>
        <div v-show="cur == 2">
          <div class="song-list">
            <div class="list-item" v-for="item of songList" :key="item.id">
              <div class="song-name">
                <div class="img">
                  <img v-lazy="item.coverImgUrl" alt="" />
                </div>
                <div class="list-name">
                  <a href="##" @click="handleList(item.id)">{{ item.name }}</a>
                </div>
              </div>
              <div class="singer singer-bot">
                <div class="trackcount">{{ item.trackCount }}首</div>
                <div class="nickname">by {{ item.creator.nickname }}</div>
              </div>
              <div class="album collect">收藏：{{ item.bookCount }}</div>
              <div class="timer">收听：{{ item.playCount | handleNum }}万</div>
            </div>
          </div>
        </div>
        <div v-show="cur == 3">
          <div class="videos">
            <div
              class="video-item"
              v-for="item of videos"
              :key="item.id"
              @click="handleMv(item.vid)"
            >
              <img v-lazy="item.coverUrl" alt="" />
              <h4>{{ item.title | handleString }}</h4>
              <p>{{ item.creator[0].userName }}</p>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      input: "",
      cur: 0,
      single: [],
      song: [],
      songList: [],
      videos: [],
    };
  },
  mounted() {
    var { key } = this.$route.query;
    this.input = key;
    var keys = this.input;
    this.$http.get(`/search?keywords=${keys}`).then((res) => {
      this.single = res.data.result.songs;
    });
    this.$http.get(`/search?keywords=${keys}&type=100`).then((res) => {
      this.song = res.data.result.artists;
    });
    this.$http.get(`/search?keywords=${keys}&type=1000`).then((res) => {
      // console.log(res);
      this.songList = res.data.result.playlists;
    });
    this.$http.get(`/search?keywords=${keys}&type=1014`).then((res) => {
      console.log(res);
      this.videos = res.data.result.videos;
    });
  },
  methods: {
    handlePaly(val) {
      this.$https.post("/playmusic", { val }).then((res) => {
        console.log(res);
      });
      this.$bus.$emit("shuaxin");
    },
    handleSinger(val) {
      this.$router.push(`/singerdetail?id=${val}`);
    },
    handleList(val) {
      this.$router.push(`/songdetail?id=${val}`);
    },
    handleMv(val) {
      this.$router.push(`/mvdetail?id=${val}`);
    },
    handleDetail(val) {
      this.$router.push(`/play?id=${val}`);
    },
    handleSearch() {
      // console.log(this.input);
      var keys = this.input;
      this.$http.get(`/search?keywords=${keys}`).then((res) => {
        this.single = res.data.result.songs;
      });
      this.$http.get(`/search?keywords=${keys}&type=100`).then((res) => {
        this.song = res.data.result.artists;
      });
      this.$http.get(`/search?keywords=${keys}&type=1000`).then((res) => {
        // console.log(res);
        this.songList = res.data.result.playlists;
      });
      this.$http.get(`/search?keywords=${keys}&type=1014`).then((res) => {
        // console.log(res);
        this.videos = res.data.result.videos;
      });
    },
        handleCollect(val) {
      console.log(val);
      this.$https.post("/musiccollect", { val }).then((res) => {
        console.log(res);
      });
    },
  },
};
</script>

<style scoped>
.search {
  width: 1200px;
  margin: 0 auto;
  margin-top: 30px;
}
.search-input {
  display: flex;
  width: 450px;
  padding-top: 20px;
  margin: 0 auto;
}
.search-input div {
  text-align: center;
  width: 50px;
  line-height: 40px;
  border: 1px solid #c9c9c9;
  border-radius: 0 10px 10px 0;
}
input {
  outline: none;
  border: 1px solid #c9c9c9;
  width: 400px;
  line-height: 40px;
  border-radius: 10px 0 0 10px;
}
.search-conent {
  margin-top: 60px;
}
ul li {
  margin: 0;
  padding: 0;
  list-style: none;
}
#app {
  width: 600px;
  height: 400px;
  margin: 0 auto;
  border: 1px solid #ccc;
}
.tab-tilte {
  overflow: hidden;
}
.tab-tilte li {
  float: left;
  width: 25%;
  padding: 10px 0;
  text-align: center;
  background-color: #f4f4f4;
  cursor: pointer;
}
.tab-content {
  padding-top: 30px;
}
/* 点击对应的标题添加对应的背景颜色 */
.tab-tilte .active {
  background-color: #09f;
  color: #fff;
}
.single {
  font-size: 14px;
  display: flex;
  padding: 0px 30px;
  line-height: 50px;
  /* justify-content: space-between; */
}
.single:nth-child(2n) {
  background-color: #f7f7f7;
}
.single:hover {
  background-color: #f7f7f7;
}
.name {
  display: flex;
  justify-content: space-between;
  width: 500px;
}
.box {
  width: 70px;
  display: none;
  margin-right: 40px;
}
.single:hover .box {
  display: block;
}
.box span {
  margin-left: 10px;
}
.name a:hover {
  color: #09f;
}
.singer {
  width: 300px;
}
.album {
  width: 220px;
}
.time {
  color: #c9c9c9;
  width: 100px;
}
.song {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 10px 30px;
}
.song-name {
  width: 500px;
}
.song-item {
  width: 150px;
  height: 200px;
  margin-right: 20px;
}
.song-item p {
  margin-top: 10px;
}
.song-item img {
  width: 150px;
  height: 150px;
}
.song-list {
  padding: 10px 30px;
}
.list-item {
  display: flex;
  align-items: center;
  height: 50px;
  padding: 10px 0;
  font-size: 14px;
}
.list-name:hover {
  color: #09f;
}
.videos {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 10px 30px;
}
.video-item {
  margin-right: 3px;
  height: 150px;
}
.video-item img {
  width: 160px;
  height: 100px;
}
.video-item p {
  margin-top: 10px;
  font-size: 14px;
}
.song-name .img img {
  width: 60px;
  height: 60px;
}
.song-name {
  display: flex;
}
.list-name {
  line-height: 50px;
  margin-left: 30px;
}
.singer-bot {
  display: flex;
}
.trackcount {
  width: 100px;
  color: #999999;
}
.collect {
  color: #999999;
}
.timer {
  color: #999999;
}
.nickname {
  width: 100px;
  margin-left: 50px;
  font-size: 12px;
  color: #666666;
}
.box .icon-jiahao:hover{
  color: #09f;
}
.box .icon-xiai:hover{
  color: red;
}
</style>